package com.anysoft.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

/**
 * Created by lty on 2017/12/3.
 */

public class CommonUtil {
    
    private static long lastClickTime;
    private static String versionName;
    
    /**
     * 防止暴力点击
     *
     * @return
     */
    public static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        if (time - lastClickTime < 1000) {
            return true;
        }
        lastClickTime = time;
        return false;
    }
    
    /**
     * 版本名，如：3.5.2
     */
    public static String getVersionName(Context context) {
        if (versionName == null) {
            versionName = getPackageInfo(context).versionName;
        }
        return versionName;
    }
    
    public static PackageInfo getPackageInfo(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public static String getFileExtension(String url) {
        String str = "";
        
        if (TextUtils.isEmpty(url)) {
            return str;
        }
    
        String urlstr1 = url.split("\\?")[0];
        int i = urlstr1.lastIndexOf("/");
        if (i <= -1) {
            return str;
        }
        String urlstr2 = urlstr1.substring(i + 1);
        int indexOf = urlstr2.lastIndexOf(".");
        if (indexOf <= -1) {
            return str;
        }
        str = urlstr2.substring(indexOf + 1);
        return str;
    }
    
    public static String getFileNameHasExtension(String url) {
        String str = "";
        
        if (TextUtils.isEmpty(url)) {
            return str;
        }
    
        String urlStr = url.split("\\?")[0];
        int i = urlStr.lastIndexOf("/");
        if (i <= -1) {
            return str;
        }
        str = urlStr.substring(i + 1);
        return str;
    }
    
    public static String getFileNameNoExtension(String url) {
        String str = "";
        
        if (TextUtils.isEmpty(url)) {
            return str;
        }
    
        String urlStr1 = url.split("\\?")[0];
        int i = urlStr1.lastIndexOf("/");
        if (i <= -1) {
            return str;
        }
        String urlStr2 = urlStr1.substring(i + 1);
        int lastIndexOf = urlStr2.lastIndexOf(".");
        if (lastIndexOf <= -1) {
            return str;
        }
    
        str = urlStr2.substring(0, lastIndexOf);
        return str;
    }
    
}
